
package com.controller;

import com.POJO.*;
import com.service.GradeService;
import com.service.StudentClassService;
import com.service.SubjectService;
import com.service.TeacherSubjectClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Package com.controller
 * @Date 2024-05-23 11:59
 * @Description 教师管理的控制器类，处理前端关于教师相关请求
 */
@Controller
@RequestMapping("/teachers")
public class TeacherController {
    @Autowired
    private TeacherSubjectClassService teacherSubjectClassService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private GradeService gradeService;
//    @Autowired
//    private StudentClassService studentClassService;


    /**
     * 获取教师的所有授课信息
     *
     * @param teacherId 教师ID
     * @return 包含教师授课信息的视图
     */
    @GetMapping("/list/{teacherId}")
    public ModelAndView getTeacherById(@PathVariable("teacherId") int teacherId) {
        List<TeacherSubjectClass> teachers = teacherSubjectClassService.getTeacherSubjectClassByTeacherId(teacherId);
        ModelAndView modelAndView = new ModelAndView("teacherHome");
        modelAndView.addObject("teacher", teachers.get(0));
        return new ModelAndView("teacherHome");
    }

    /**
     * 新增教师-科目-班级关系
     *
     * @param teacherId 教师ID
     * @param subjectId 科目ID列表
     * @param classId   班级ID列表
     * @return 重定向到教师详情视图
     */
    @PostMapping("/{teacherId}/subjectId/classId")
    public ModelAndView insertTeacherSubjectClass(@PathVariable int teacherId, @RequestParam List<Integer> subjectId, @RequestParam List<Integer> classId) {
        TeacherSubjectClass teacherSubjectClass = new TeacherSubjectClass();
        teacherSubjectClass.setTeacherId(teacherId);
        // 根据subjectId和classId列表分别创建TeacherSubjectClass对象并插入
        for (Integer subId : subjectId) {
            for (Integer clsId : classId) {
                TeacherSubjectClass tsc = new TeacherSubjectClass();
                tsc.setTeacherId(teacherId);
                tsc.setSubjectId(subId);
                tsc.setClassId(clsId);
                teacherSubjectClassService.insertTeacherSubjectClass(tsc);
            }
        }
        ModelAndView mv = new ModelAndView("redirect:/teachers/list/" + teacherId);
//        mv.addObject("message", "Subjects and classes inserted successfully!");
        return mv;
    }

    /**
     * 跳转到添加或更新科目视图
     *
     * @param teacherId 教师ID
     * @return 添加或更新科目视图
     */
    @GetMapping("/{teacherId}/subjects/new")
    public ModelAndView addOrUpdateSubject(@PathVariable("teacherId") int teacherId) {
        List<Subject> subjects = subjectService.getSubjectsTeacherId(teacherId);
        List<TeacherSubjectClass> teacherSubjectClasses = teacherSubjectClassService.getTeacherSubjectClassByTeacherId(teacherId);
        ModelAndView mv = new ModelAndView("teacherSubjects");
        mv.addObject("teacherId", teacherId);
        mv.addObject("subjects", subjects);
        mv.addObject("teacherSubjectClasses", teacherSubjectClasses);
        return mv;
    }

    /**
     * 删除教师-科目-班级关系
     *
     * @param id        关系ID
     * @param teacherId 教师ID
     * @return 重定向到教师详情视图
     */
    @PostMapping("/{teacherId}/delete/{id}")
    public ModelAndView deleteTeacherSubjectClass(@PathVariable("id") int id, @PathVariable("teacherId") int teacherId) {
        teacherSubjectClassService.deleteTeacherSubjectClass(id);
        return new ModelAndView("redirect:/teachers/list/" + teacherId);
    }


    /**
     * 跳转到教师录入成绩页面
     * @param request
     * @return teacherGrades视图
     */
    @GetMapping("/grades")
    public ModelAndView toTeacherGrades(HttpServletRequest request) {
        User teacher = (User) request.getSession().getAttribute("user");
        ModelAndView modelAndView = new ModelAndView("teacherGrades");

        List<Subject> subjects = subjectService.getSubjectsTeacherId(teacher.getUserId());

        modelAndView.addObject("teacher", teacher);
        modelAndView.addObject("subjects", subjects);
        return modelAndView;
    }

    /**
     * 获取科目ID
     * 在教师登录成绩页面显示对应科目下学生成绩列表
     * @param subjectId
     * @return  teacherGrades视图
     */

    /**
     * 获取科目ID
     * 在教师登录成绩页面显示对应科目下学生成绩列表
     * @param subjectId
     * @return  teacherGrades视图
     */
    @GetMapping("/{teacherId}/subjects/{subjectId}/grades")
    @ResponseBody
    public List<Grade> getGradesBySubjectId(@PathVariable("teacherId") int teacherId, @PathVariable("subjectId") int subjectId) {
        return gradeService.getGradesBySubjectId(subjectId);
    }

    /**
     * 新增成绩（成绩录入）
     * @param grade  成绩分数
     * @return teacherGrades视图
     */
    @PostMapping("/grades/add")
    @ResponseBody
    public String addGrade(@RequestBody GradeAdd grade) {
        gradeService.addGrade(grade); //添加成绩
        return "Grades added successfully!";
    }


    /**
         * 跳转到教师首页
         *
         * @return teacherHome视图
         */
    @GetMapping("/")
    public ModelAndView toTeacherHome(HttpServletRequest request) {
        User teacher = (User) request.getSession().getAttribute("user");
        ModelAndView modelAndView = new ModelAndView("teacherHome");
        modelAndView.addObject("teacher", teacher);
        return modelAndView;

    }

}
